<template>
  <el-dialog
    :title="!dataForm.id ? '新增' : '修改'"
    :close-on-click-modal="false"
    :visible.sync="visible">
    <el-form :model="dataForm" inline :rules="dataRule" ref="dataForm" @keyup.enter.native="dataFormSubmit()" label-width="120px">
    <el-form-item label="气温" prop="airTemperature">
      <el-input v-model="dataForm.airTemperature" placeholder="气温"></el-input>
    </el-form-item>
    <el-form-item label="天气" prop="weather">
      <el-input v-model="dataForm.weather" placeholder="天气"></el-input>
    </el-form-item>
    <el-form-item label="风速" prop="windSpeed">
      <el-input v-model="dataForm.windSpeed" placeholder="风速"></el-input>
    </el-form-item>
    <el-form-item label="空气" prop="airQuality">
      <el-input v-model="dataForm.airQuality" placeholder="空气"></el-input>
    </el-form-item>
    <el-form-item label="地区" prop="area">
      <el-input v-model="dataForm.area" placeholder="地区"></el-input>
    </el-form-item>
    </el-form>
    <span slot="footer" class="dialog-footer">
      <el-button @click="visible = false">取消</el-button>
      <el-button type="primary" @click="dataFormSubmit()">确定</el-button>
    </span>
  </el-dialog>
</template>

<script>
  export default {
    data () {
      return {
        visible: false,
        dataForm: {
          id: 0,
          airTemperature: '',
          weather: '',
          windSpeed: '',
          airQuality: '',
          area: '',
        },
        dataRule: {
          airTemperature: [
            { required: true, message: '气温不能为空', trigger: 'blur' }
          ],
          weather: [
            { required: true, message: '天气不能为空', trigger: 'blur' }
          ],
          windSpeed: [
            { required: true, message: '风速不能为空', trigger: 'blur' }
          ],
          airQuality: [
            { required: true, message: '空气不能为空', trigger: 'blur' }
          ],
          area: [
            { required: true, message: '地区不能为空', trigger: 'blur' }
          ],
        }
      }
    },
    methods: {
      init (id) {
        this.dataForm.id = id || 0
        this.visible = true
        this.$nextTick(() => {
          this.$refs['dataForm'].resetFields()
          if (this.dataForm.id) {
            this.$http({
              url: this.$http.adornUrl(`/buss/bussweatherrecord/info/${this.dataForm.id}`),
              method: 'get',
              params: this.$http.adornParams()
            }).then(({data}) => {
              if (data && data.code === 0) {
                this.dataForm.airTemperature = data.bussweatherrecord.airTemperature
                this.dataForm.weather = data.bussweatherrecord.weather
                this.dataForm.windSpeed = data.bussweatherrecord.windSpeed
                this.dataForm.airQuality = data.bussweatherrecord.airQuality
                this.dataForm.area = data.bussweatherrecord.area
                this.dataForm.version = data.bussweatherrecord.version
              }
            })
          }
        })
      },
      // 表单提交
      dataFormSubmit () {
        this.$refs['dataForm'].validate((valid) => {
          if (valid) {
            this.$http({
              url: this.$http.adornUrl(`/buss/bussweatherrecord/${!this.dataForm.id ? 'save' : 'update'}`),
              method: 'post',
              data: this.$http.adornData({
                'id': this.dataForm.id || undefined,
                'airTemperature': this.dataForm.airTemperature,
                'weather': this.dataForm.weather,
                'windSpeed': this.dataForm.windSpeed,
                'airQuality': this.dataForm.airQuality,
                'area': this.dataForm.area,
                'version': this.dataForm.version,
              })
            }).then(({data}) => {
              if (data && data.code === 0) {
                this.$message({
                  message: '操作成功',
                  type: 'success',
                  duration: 1500,
                  onClose: () => {
                    this.visible = false
                    this.$emit('refreshDataList')
                  }
                })
              } else {
                this.$message.error(data.msg)
              }
            })
          }
        })
      }
    }
  }
</script>
